/***********************************************************************************
@file  
@author 
@brief  
***********************************************************************************/
#ifndef __SYSTEM_H__
#define __SYSTEM_H__
/* Private includes ----------------------------------------------------------*/
#include "main.h"
/* Private define ------------------------------------------------------------*/
//#define USE_ADXL345  


//#define ZHANSHI          

#define SOFT_VERSION (sysVER_MONTH*100+sysVER_DAY)
#define ADC_SENSOR_L (500)

//#define RESPONSE_FAST 

#if defined ONCE_BATTERY
#define ENERGY_FULL  3600
#define ENERGY_DISPLAY 0
#else
#define ENERGY_FULL  4000
#define ENERGY_DISPLAY 1
#endif

#define SWAP32_(l)   ((((l)>>24)&0x000000FF) |(((l>>8)&0x0000FF00))|((l<<8)&0x00FF0000)|(((l)<<24)&0xFF000000))
#define SGB_ON       HAL_GPIO_ReadPin(LED__GPIO_Port,LED__Pin)
#define SENSOR_ON    HAL_GPIO_ReadPin(EN_SENSOR_GPIO_Port,EN_SENSOR_Pin)
 
#define DIS_RESET_TIME            60000             //显示屏复位时间
#define CHECKTIME                 50

#define TIME_NEXT_SEND            50
#define TIME_SEND_LORA            3600
#define TIME_ANALOG_PREHEAT       500
#define TIME_PERFORM_WAIT         6000
#define TIME_STOP_IDLE_INFRARED   5                  //5S
#define TIME_DATA_CHANGE          100
#define TIME_OVERFLOW_WAIT        3000
#define TIME_ATTENNUATION         60480000      //单位 ms 一周                           
#define TIME_LORA_CONFIG          86400         //RTC定时器(24*60*60)               
#define TIME_BG_ON                3000

#define TIMES_POWER_LOW10         10
#define TIMES_POWER_LOW5          10
#define TIMES_POWER_ZERO          3

#define PERFORM_TYPE_NONE         0
#define PERFORM_TYPE_6969         1
#define PERFORM_TYPE_6868         2
#define HOT_NUMBER                10

#define TRUE_ON                   1
#define FALSE_OFF                 0
/* Private typedef -----------------------------------------------------------*/
#pragma         pack(1)
typedef struct                                   //探测器返回数据格式
{
    uint8_t  gas;                                //气体种类
    uint16_t value;                              //浓度值
    uint8_t  propor  :4;                         //比例
    uint8_t  unit    :4;                         //单位
    uint8_t  state;                              //探测器状态
}_Sensor_data_struct; 

typedef struct                                   //探测器返回数据格式
{
    uint8_t  head;
    uint16_t soft_version;
    uint8_t  myaddress; 
    uint8_t  preheating_time; 
    uint8_t  unit; 
    uint8_t  precision; 
    uint8_t  negative; 
    uint8_t  gastype; 
    uint8_t  sensor_mode;
    uint8_t  lora_channel; 
    uint8_t  lora_strength; 
    uint8_t  alarm_times;
    uint8_t  mobility;
    uint8_t  angle_alarm;
    uint8_t  speak;
    uint8_t  energy_flag;
    uint8_t  data_stability;
    uint16_t energy_full;
    uint16_t low_alarm;                              
    uint16_t high_alarm;
    uint16_t range_ability;
    uint16_t zero;
    uint16_t kuadu[3];
    uint16_t adc[3];
    uint16_t host_address[5];
    uint16_t hot_number;
    uint16_t power_interval;  
    uint16_t speak_times;
    
    uint16_t crc;    
}_Data_store_struct; 
#pragma         pack()
/* Private variables ---------------------------------------------------------*/

extern _Data_store_struct system_data;

extern uint16_t myadc[4];
extern uint16_t adcdata;                                                              //ADC采样的值
extern uint16_t adc_sensor; 
extern uint16_t mydata;
extern uint8_t status;
extern int16_t temperature;                                                           
extern uint8_t humidity;
extern uint16_t vrefint_cal;       
extern uint16_t vbat;
extern uint8_t energy;	    													      //电池容量
extern uint8_t power_low1;	    													  //电池容量
extern uint8_t power_low2;	    													  //电池容量
extern uint8_t power_long;

extern uint8_t flag_stop;                                                             //进入STOP标志



extern uint8_t goto_lowpower;
extern uint8_t start_averrage;
extern uint8_t dis_lowpower;
extern uint8_t notic_lowpower;
extern uint8_t perform_type;
extern uint8_t flag_perform_over;

extern int16_t  temporary_value_s16;
extern uint16_t temporary_value_u16;                                                
extern uint16_t temporary_value_u16_1;                                             
extern uint32_t temporary_value_u32;                                               
extern int32_t temporary_value_s32;                                              

extern uint8_t flag_enter_first;
extern uint8_t data_change;                                                        //数据变化
extern uint8_t lora_ok;                                                            //lora标识
extern uint8_t  xiaoyin;                                                           //消音标志
extern uint8_t gate_in;														       //密码入口
extern uint8_t  key;                                                               //按键值
extern uint16_t password_in;                                                       //临时值
extern uint8_t flag_ex_wakeup;                                                     //外部唤醒标志
extern uint8_t flag_ex_wakeup2;                                                    //外部唤醒标志
extern uint8_t ceshi;						 							         //测试状态
extern uint8_t stop_stats_time;                                                  //休眠时间
extern uint16_t preheating;													     //开机预热
extern uint8_t startCount;														 //开始计时

//定时器
extern uint16_t gzjs;															//传感器脱落计时
extern uint8_t  activity;                                                       //活动界面
extern uint16_t dead_time;                                                      //无操作时间
extern uint16_t dead_time_th;                                                   //无操作时间阈值

extern int64_t time_send_lora;                                                 //lora
extern int64_t time_lora_config;                                               //发送配置计时

extern uint16_t time_data_change;
extern uint16_t time_bg_on;
extern uint8_t test_num;

extern uint16_t time_get_analog;                                                 //获取AD值
extern uint32_t time_attenuation;                                                //衰减计时
extern uint32_t time_perform_wait;                                           


extern uint8_t state_hot;


extern uint8_t ndir_tb[30];                                                       	  //串口三发送缓存
extern uint8_t ndir_tlen;                                                          	  //串口三要发送的数据长度
extern uint8_t ndir_receive_flag;                                                	  //串口三接收数据标志
extern uint8_t ndir_receive_ok;                                                 		  //串口三接受完成标志
extern uint8_t ndir_rlen;                                                        		  //串口三接收到的数据长度
extern uint8_t ndir_receive_cnt;									    				  //接收到数据开始计时
extern uint8_t ndir_rb[50];                                                             //串口三接收缓存
extern uint8_t receive_mode;													      //接受模式
extern uint8_t sensor[5];               												  //指令
extern uint8_t wait_sensor;														  //等待传感器回信息
extern uint8_t wait_sensor_time;												      //等待传感器回信息时间
extern uint8_t wait_time;											    	    	  //传感器未回数据次数


/* Private function ----------------------------------------------------------*/
void delay_100us(uint32_t delay);
void delay_ms(uint32_t delay);

void system_wdg_refresh(void);
void system_start(void);
void system_data_process(void);
void system_data_process_sleep(void);
void system_power_on(void);

void system_adc_update(void);
void system_adc_preheat(void);
void system_adc_attenuation(void);
void system_power_status(void);
void system_adc_attenuation_timer(uint8_t fun);
void data_save(void);
void data_save_init(void);
void system_timer(void);

#define sysVER_MONTH ( \
    __DATE__[0] == 'J' ? (__DATE__[1] == 'a' ? 1 : (__DATE__[2] == 'n' ? 6 : 7)) /* Jan, Jun, Jul */ \
    : __DATE__[0] == 'F' ? 2 /* Feb */ \
    : __DATE__[0] == 'M' ? (__DATE__[2] == 'r' ? 3 : 5) /* Mar, May */ \
    : __DATE__[0] == 'A' ? (__DATE__[1] == 'p' ? 4 : 8) /* Apr, Aug */ \
    : __DATE__[0] == 'S' ? 9 /* Sep */ \
    : __DATE__[0] == 'O' ? 10 /* Oct */ \
    : __DATE__[0] == 'N' ? 11 /* Nov */ \
    : __DATE__[0] == 'D' ? 12 /* Dec */ \
    : 0 /* 默认值,避免未匹配 */ \
)


#define sysVER_DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
    + (__DATE__ [5] - '0'))
    
#endif



